#!/usr/bin/perl
#integrality-lib.pl

do '../web-lib.pl';
&init_config();

sub revalidate_sharetab
{
	my %chklist;

	####cifs check
	&foreign_require("samba", "samba-lib.pl");
	%econfig = &foreign_config("samba");
	@cifsshares = &foreign_call("samba", "list_shares");
	foreach $s (@cifsshares) {
		if (($s eq "global")||($s eq "homes")) { next; }
		&foreign_call("samba", "get_share", $s);
		$cifspath = &foreign_call("samba", "getval", "path");
		next if(!$cifspath);
		$comment = &foreign_call("samba", "getval", "comment");
		$chklist{$cifspath} = {name => $s, type => 'W', comment => $comment};
	}

	####nfs check
	&foreign_require("exports", "exports-lib.pl");
	%econfig = &foreign_config("exports");
	@nfsshares = &foreign_call("exports", "list_exports");
	@nfspaths = map($_->{'dir'}, @nfsshares);
	@nfsdiff = grep { $marked{$_}++; $marked{$_} == 1; } @nfspaths;
	foreach $n (@nfsdiff) {
		if (exists($chklist{$n}))
		{
			$chklist{$n}{'type'} .= "U";
		}
		else
		{
			$chklist{$n} ={name => '', type => 'U', comment =>''};
		}
	}

	####ftp check
	&foreign_require("proftpd", "proftpd-lib.pl");
	%econfig = &foreign_config("proftpd");
	my $fconf = &foreign_call("proftpd", "get_virtual_config", undef);
	my @ftppaths = &foreign_call("proftpd", "find_directive_struct", "DefaultRoot", $fconf);
	my @anon = &foreign_call("proftpd", "find_directive_struct", "Anonymous", $fconf);
	@ftppaths = map($_->{'words'}->[0], (@ftppaths, @anon));
	@ftpdiff = grep { $fmarked{$_}++; $fmarked{$_} == 1; } @ftppaths;
	foreach $ff (@ftpdiff) {
		if (exists($chklist{$ff}))
		{
			$chklist{$ff}{'type'} .= "F";
		}
		else
		{
			if ($ff eq "$text{'nodir'}")
			{
				next;
			}
			else
			{
				$chklist{$ff} ={name => '', type => 'F', comment =>''};
			}
		}
	}

	####http check
	&foreign_require("apache", "apache-lib.pl");
	%econfig = &foreign_config("apache");
	my $hconf = &foreign_call("apache", "get_virtual_config", undef);
	my @droots = &foreign_call("apache", "find_directive_struct", "DocumentRoot", $hconf);
	if (@droots)
	{
		$f = $droots[0]->{'file'};
		$droot = $droots[0]->{'words'}->[0];
		@dir = &foreign_call("apache", "find_directive_struct", "Directory", $hconf);
		if (@dir)
		{
			&lock_file($f);
			$httpref = undef;
			foreach $d (@dir){
				$d->{'words'}->[0] =~ /^$droot\/(.*)$/;
				$httpname = $1;
				if (-l $d->{'words'}->[0])
				{
					my $res = readlink $d->{'words'}->[0];
					if (exists($chklist{$res}))
					{
						if (lc($httpname) ne lc($chklist{$res}{'name'}))
						{
							if (!defined($httpref))
							{
								$httpref = &read_file_lines($f);
							}
							$httpref->[$d->{'line'}] = "<Directory \"$droot\/$chklist{$res}{'name'}\">";
							`rm -f $droot\/$httpname`;
							`ln -s $res $droot\/$chklist{$res}{'name'}`;

						}
						$chklist{$res}{'type'} .= "H" if ($chklist{$res}{'type'} !~ /H/);
					}
					else
					{
						$chklist{$res} ={name => $httpname, type => 'H', comment =>''};
					}
				}
			}
			if (defined($httpref))
			{
				&flush_file_lines();
			}
			&unlock_file($f);
		}
	}

	#sharetab check
	my %oritab;
	&foreign_require("shareman", "shareman-lib.pl");
	&foreign_call("shareman", "read_sharetab", \%oritab, 2);
#	&read_sharetab(\%oritab, 2);
	open(SHARETAB, "> $config{'sharetab_file'}");

	$basenum = time();
	foreach $path (keys %chklist) {
		if ($chklist{$path}{'name'} eq "")
		{
			if (exists($oritab{$path}))
			{
				$chklist{$path}{'name'} = $oritab{$path}{'sharename'};
			}
			else
			{
				my $newname = $basenum;
				$basenum++;
				$chklist{$path}{'name'} = "share".$newname;

			}
		}
		print SHARETAB "$path:$chklist{$path}{'name'}:$chklist{$path}{'type'}:$chklist{$path}{'comment'}\n";
	}

	close(SHARETAB);

}

1;
